<template>
  <div class="form">
    <h2>注册</h2>
    <el-form
      style="margin-top: 40px"
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      status-icon
      label-position="top"
    >
      <el-form-item prop="username" label="用户名">
        <el-input v-model="ruleForm.username" size="large"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          v-model="ruleForm.password"
          size="large"
          type="password"
          show-password
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item prop="checkPassword" label="确认密码">
        <el-input
          v-model="ruleForm.checkPassword"
          size="large"
          type="password"
          show-password
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="large"
          style="width: 100%"
          @click="submitForm(ruleFormRef)"
        >
          注册
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="large" style="width: 100%" @click="goto">
          已有账号，去登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import router from "../../router";
import { register } from "../../api/userLogin/index";
import { ElMessage, ElMessageBox } from "element-plus";
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  username: "",
  password: "",
  checkPassword: "",
});
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    if (ruleForm.checkPassword !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPassword", () => null);
    }
    callback();
  }
};
const validateCheckPass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入确认密码"));
  } else if (value !== ruleForm.password) {
    callback(new Error("两次密码不一致"));
  } else {
    callback();
  }
};
const rules = reactive<FormRules>({
  username: [
    {
      required: true,
      message: "请输入账号",
      trigger: "blur",
    },
  ],
  password: [
    { validator: validatePass, trigger: "blur" },
    { min: 6, max: 20, message: "密码在6-20为之间", trigger: "blur" },
  ],
  checkPassword: [{ validator: validateCheckPass, trigger: "blur" }],
});

//注册
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const res = await register(ruleForm);
      if (res.data.success) {
        ElMessage({
          message: res.data.msg,
          type: "success",
        });
        router.push({ path: "/login" });
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};

const goto = () => {
  router.push("/login/userLogin");
};
</script>

<style lang="scss" scoped>
.form {
  width: 400px;
  height: 500px;
  text-align: center;
  padding: 20px;
}
</style>
